﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using LEDScreenLinkTool.Model;
using LEDScreenLinkTool.Util;
using LEDScreenLinkTool.ViewModel;
using Panuon.WPF.UI;
using log4net.Appender;

namespace LEDScreenLinkTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : WindowX
    {
        private NovaConnectViewModel viewModel;
        public MainWindow()
        {
            InitializeComponent();
            NovaInitialize();
        }

        public void NovaInitialize()
        {
            viewModel = new NovaConnectViewModel();
            this.DataContext = viewModel;

        }

        private void Screen_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (IsLoaded)
            {
                viewModel.ScreenHeight = (int)mainBoard.ActualHeight;
                viewModel.ScreenWidth = (int)mainBoard.ActualWidth;
                viewModel.RemakeModules(out string pictureName);
                if (!string.IsNullOrEmpty(pictureName))
                {
                    ImageBrush imgBrush = new ImageBrush();
                    imgBrush.ImageSource = new BitmapImage(new Uri($"Screens/{pictureName}", UriKind.Relative));
                    imgBrush.Stretch = Stretch.Fill;
                    mainBoard.Background = imgBrush;
                }
                viewModel.GetConnectSolutions();
                viewModel.RefreshLine();
            }
        }

        private void MainWin_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel.ScreenHeight = (int)mainBoard.ActualHeight;
            viewModel.ScreenWidth= (int)mainBoard.ActualWidth;
           
            viewModel.RemakeModules(out string pictureName);
            if (!string.IsNullOrEmpty(pictureName))
            {
                ImageBrush imgBrush = new ImageBrush();
                imgBrush.ImageSource = new BitmapImage(new Uri($"Screens/{pictureName}", UriKind.Relative));
                imgBrush.Stretch = Stretch.Fill;
                mainBoard.Background = imgBrush;
            }
        }

        private void MainWin_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            viewModel.Dispose();
        }

        /// <summary>
        /// 灯板尺寸切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ModuleSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (IsLoaded)
            {
                viewModel.ModuleSizeChange();
            }
        }

    
        /// <summary>
        /// 整屏尺寸变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Resize_BtnClk(object sender, RoutedEventArgs e)
        {
            var callResult = EErrorCode.Failure;
            var pendingBox = MessageBoxHelper.PendingBox(sender, LoadingText: "Loading");
            this.Dispatcher.Invoke(new Action(() =>
            {
                callResult = viewModel.ResizeScreenBody(out string pictureName);
                if (!string.IsNullOrEmpty(pictureName))
                {
                    ImageBrush imgBrush = new ImageBrush();
                    imgBrush.ImageSource = new BitmapImage(new Uri($"Screens/{pictureName}", UriKind.Relative));
                    imgBrush.Stretch = Stretch.Fill;
                    mainBoard.Background = imgBrush;
                }
                Thread.Sleep(500);
            }));
            pendingBox.Close();
            if (callResult!= EErrorCode.Success)
            {
                MessageBoxHelper.WarningBox(sender, callResult);
            }
        }



        /// <summary>
        /// 手动设置起始点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainCv_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var clkPoint = e.GetPosition(mainCv);
            double posX = clkPoint.X;
            double posY = clkPoint.Y;
            var callResult = false;
            var pendingBox = MessageBoxHelper.PendingBox(sender, LoadingText: "Loading");
            this.Dispatcher.Invoke(new Action(() =>
            {
                callResult = viewModel.DrawStartLine(posX, posY);
            }));
            pendingBox.Close();
            if (!callResult)
            {
                MessageBoxHelper.WarningBox(sender, EErrorCode.InputError);
            }
        }

        #region 连线方案切换
        private void Pre_BtnClk(object sender, RoutedEventArgs e)
        {
            viewModel.PreLine();
        }

        private void Next_BtnClk(object sender, RoutedEventArgs e)
        {
            viewModel.NextLine();
        }

        private void CmbLine_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(IsLoaded)
            {
                viewModel.ChangeDisplayLink(mainCv);
            }
        }
        #endregion


    }
}
